package vue;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.util.Map.Entry;

import javax.swing.*;

import tools.ChessImageProvider;
import model.Coord;
import controler.ChessGameControlers;
import controler.controlerLocal.ChessGameControler;
 
public class ChessGameGUI extends JFrame implements MouseListener, MouseMotionListener,Observer {
  JLayeredPane layeredPane;
  JPanel chessBoard;
  JLabel chessPiece;
  int xAdjustment;
  int yAdjustment;
  HashMap<Integer, JPanel> tableauCorrespondanceCoordonnees; 
  int[][] tableauCorrespondanceComponent;
  Component cDepart, cArrivee;
  ChessGameControler chessGameControler;
  Container chessPieceToCapture;
  
  public ChessGameGUI(String titre, ChessGameControlers chessGameControler, Dimension dim){
	  Dimension boardSize = new Dimension(dim);
	  tableauCorrespondanceCoordonnees = new HashMap<Integer, JPanel>();
	  tableauCorrespondanceComponent = new int[8][8];
	  this.chessGameControler = (ChessGameControler) chessGameControler;
	 
	  //  Use a Layered Pane for this this application
	 layeredPane = new JLayeredPane();
	  getContentPane().add(layeredPane);
	  layeredPane.setPreferredSize(boardSize);
	  layeredPane.addMouseListener(this);
	  layeredPane.addMouseMotionListener(this);
	
	  //Add a chess board to the Layered Pane 
	 
	  chessBoard = new JPanel();
	  layeredPane.add(chessBoard, JLayeredPane.DEFAULT_LAYER);
	  chessBoard.setLayout( new GridLayout(8, 8) );
	  chessBoard.setPreferredSize( boardSize );
	  chessBoard.setBounds(0, 0, boardSize.width, boardSize.height);
	 
	  for (int i = 0; i < 64; i++) {
	  JPanel square = new JPanel( new BorderLayout() );
	  chessBoard.add( square );
	 
	  int row = (i / 8) % 2;
	  if (row == 0)
	  square.setBackground( i % 2 == 0 ? Color.white : Color.gray );
	  else
	  square.setBackground( i % 2 == 0 ? Color.gray : Color.white );
	  }
	 
	  //Add a few pieces to the board
	  int cpt = 0;
	  String type, image;
	  for(int i = 0; i < 8; i++)
	  {
		  for(int j = 0; j < 8; j++)
		  {
			  JPanel panel = (JPanel)chessBoard.getComponent(cpt);
			  tableauCorrespondanceCoordonnees.put(cpt,panel);
			  
			  type = ChessImageProvider.getType(i,j);
			  if(type != null)
			  {
				  image = ChessImageProvider.getImageFile(type, ChessImageProvider.getCouleur(i,j));
				  JLabel piece = new JLabel( new ImageIcon(image) );
				  panel.add(piece);
			  }
			  //tableauCorrespondanceComponent[j][i] = cpt;
			  
			  cpt++;
		  }
	  }

  }
 
  public void mousePressed(MouseEvent e){
	  chessPiece = null;
	  Component c = chessBoard.findComponentAt(e.getX(), e.getY());
	  cDepart = c.getParent();
	  	 
	  if (c instanceof JPanel) 
	  return;
	 
	  Point parentLocation = c.getParent().getLocation();
	  xAdjustment = parentLocation.x - e.getX();
	  yAdjustment = parentLocation.y - e.getY();
	  chessPiece = (JLabel)c;
	  chessPiece.setLocation(e.getX() + xAdjustment, e.getY() + yAdjustment);
	  chessPiece.setSize(chessPiece.getWidth(), chessPiece.getHeight());
	  layeredPane.add(chessPiece, JLayeredPane.DRAG_LAYER);
  }
 
  //Move the chess piece around
  //Maintient
  public void mouseDragged(MouseEvent me) {
	  if (chessPiece == null) return;
	  	chessPiece.setLocation(me.getX() + xAdjustment, me.getY() + yAdjustment);
 }
 
  //Drop the chess piece back onto the chess board
 //Relache
  public void mouseReleased(MouseEvent e) {
	  if(chessPiece == null) return;
	  chessPieceToCapture = null;
	  chessPiece.setVisible(false);
	  cArrivee =  chessBoard.findComponentAt(e.getX(), e.getY());
	  
	 
	  if (cArrivee instanceof JLabel){
		  chessPieceToCapture = (Container) cArrivee;
		  cArrivee = cArrivee.getParent();
	  }
	  
	  /*if (cArrivee instanceof JLabel){
		  Container parent = cArrivee.getParent();
		  parent.remove(0);
		  parent.add( chessPiece );
	  }
	  else {
		  Container parent = (Container)cArrivee;
		  parent.add( chessPiece );
	  }*/
	  
	  Coord coordoneeDepart = null, coordoneeArrive = null;
	  
	  for(Entry<Integer, JPanel> entry : tableauCorrespondanceCoordonnees.entrySet()) {
		  if(entry.getValue().equals(cDepart))
		  {
			  coordoneeDepart = new Coord(entry.getKey()%8, entry.getKey()/8);
		  }
		  if(entry.getValue().equals(cArrivee))
		  {
			  coordoneeArrive = new Coord(entry.getKey()%8, entry.getKey()/8);
		  }
	  }
	  
	  chessGameControler.move(coordoneeDepart, coordoneeArrive);
	  
  }
 //Au clic
  public void mouseClicked(MouseEvent e) {
  }
  //Souris bouge dans la fenêtre
  public void mouseMoved(MouseEvent e) {
 }
  //Rentre dans la fenêtre
  public void mouseEntered(MouseEvent e){
  
  }
  //Sort de la fenêtre
  public void mouseExited(MouseEvent e) {
  
  }

	@Override
	public void update(Observable arg0, Object arg1) {
		chessPiece.setVisible(true);
		Object[] tab = (Object[]) arg1;
		if(((boolean) tab[4]) != false)
		{
			if (chessPieceToCapture != null){
				((Container) cArrivee).remove(0);
				((Container) cArrivee).add( chessPiece );
			}
			else
			{
				((Container) cArrivee).add( chessPiece );
			}
		}
		  else
		  {
			  Container parent = (Container)cDepart;
			  parent.add( chessPiece );
		  }
	}
}
